﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using MaxAlarm.Alerts;
using MaxAlarm.Common;
using MaxAlarm.Common.Alarms;
using MaxAlarm.Common.Data;
using MaxAlarm.Core.Settings;
using MaxAlarm.Windows;
using Timer = System.Timers.Timer;

namespace MaxAlarm.AlarmActions
{
    public class IgnoreAlarmAction : AlarmActionBase
    {
        public override AlarmAction Action
        {
            get { return AlarmAction.Ignore; }
        }

        public IgnoreAlarmAction(AlarmItem item)
            : base(item)
        {
        }

        public override void Execute()
        {
            Item.IsExecuted = true;
            return;
        }
    }

    public class ImmediatelyAlarmAction : AlarmActionBase
    {
        public override AlarmAction Action
        {
            get { return AlarmAction.Immediately; }
        }

        public ImmediatelyAlarmAction(AlarmItem item)
            : base(item)
        {
        }

        public override void Execute()
        {
            AlertsManager.Instance.QueueItem(Item);
            Item.IsExecuted = true;
        }
    }

    public class CheckAfterAlarmAction : AlarmActionBase, INotifyPropertyChanged
    {
        private TimeSpan _waitTimer;

        public TimeSpan WaitTimer
        {
            get { return _waitTimer; }

            protected set 
            { 
                _waitTimer = value;
                OnPropertyChanged("WaitTimer");
            }
        }

        public override AlarmAction Action
        {
            get { return AlarmAction.CheckAfter; }
        }

        public CheckAfterAlarmAction(AlarmItem item)
            : base(item)
        {
        }

        public override void Execute()
        {
            var interval = GetWaitInterval();
            Item.IsExecuted = false;
            _waitTimer = TimeSpan.FromSeconds(interval);
            var worker = new BackgroundWorker();
            WaitQueueManager.Instance.Queue(this);

            worker.DoWork += delegate
                                 {
                                     while(!WaitTimer.TotalSeconds.Equals(0))
                                     {
                                         Application.Current.Dispatcher.Invoke((Action) (() =>
                                                                                             {
                                                                                                 WaitTimer = WaitTimer.Subtract(TimeSpan.FromSeconds(1));
                                                                                             }));
                                         
                                         Thread.Sleep(1000);
                                     }  
                                 };

            worker.RunWorkerCompleted += delegate
                                             {
                                                 Item.IsExecuted = true;
                                                 AlertsManager.Instance.QueueItem(Item);
                                                 WaitQueueManager.Instance.Dequeue(this);
                                             };
            worker.RunWorkerAsync();

        }

        private int GetWaitInterval()
        {
            var interval = SettingsManager.Instance.Settings.AlarmSettings[Item.AlarmType].WaitInterval;

            return interval <= 0 ? 1 : interval;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if(PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
